package cn.learn.controller.company;

import cn.learn.base.BaseController;
import cn.learn.domain.company.Company;
import cn.learn.service.company.CompanyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

//    /**
//     * 查询所有企业
//     * @return
//     */
//    @RequestMapping("/list")
//    public String selectAll(){
//        List<Company> list = companyService.selectAll();
//        request.setAttribute("list",list);
//        return "company/company-list";
//    }

    /**
     * 分页查询全部企业
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "查询全部企业")
    public String selectAll(@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageInfo list = companyService.selectAllByPage(pageNum, pageSize);
        request.setAttribute("pageList", list);
        return "company/company-list";
    }

    /**
     * 跳转到企业添加页面 与 添加企业
     * @param company
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加企业")
    public String addOne(Company company){
        if (request.getMethod().equals("POST")) {
            // 根据uuid插件包获取uid值，并字符串化，替换其中的-
            String uuid = UUID.randomUUID().toString().replace("-", "");
            company.setId(uuid);
            companyService.addOne(company);
            return "redirect:/company/list.do";
        }
        return "company/company-add";
    }

    /**
     * 更新页面跳转 与 更新内容
     * @param company
     * @return
     */
    @RequestMapping(value = "/updateOne", name = "更新企业")
    public String updateById(Company company) {
        if (request.getMethod().equals("POST")) {
            companyService.updateOneById(company);
            return "redirect:/company/list.do";
        }
        Company resultCompany = companyService.findOneById(company);
        request.setAttribute("data", resultCompany);
        return "company/company-update";
    }

    /**
     * 根据id删除一条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "删除企业")
    public String deleteById(String id) {
        companyService.deleteById(id);
        return "redirect:/company/list.do";
    }

}
